package com.home.service;

import com.home.entity.Employee;
import com.home.entity.RespPageBean;
import com.home.mapper.EmployeeMapper;
import com.home.mapper.EmployeeecMapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public RespPageBean getEmployeeByPage(Integer page, Integer size, Employee employee, Date[] beginDateScope) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<Employee> data = employeeMapper.getEmployeeByPage(page, size, employee, beginDateScope);
        Long total = employeeMapper.getTotal(employee, beginDateScope);
        RespPageBean bean = new RespPageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    public Integer addEmp(Employee employee) {
        int insert = employeeMapper.insert(employee);
        if (insert == 1) {
            Employee employee4DB = employeeMapper.getAllEmpsById(employee.getId());
            System.out.println("emp-->" + employee4DB.toString());
            rabbitTemplate.convertAndSend("email.welcome", employee4DB);
        }
        return insert;
    }

    public Integer getMaxWorkID() {
        return employeeMapper.getMaxWorkID();
    }

    public Integer deleteEmpById(Integer id) {
        return employeeMapper.deleteById(id);
    }

    public List<Employee> getAllEmps() {
        return employeeMapper.selectList(null);
    }

    public Integer editEmp(Employee employee) {
        return employeeMapper.updateById(employee);
    }

    public Integer addEmps(List<Employee> list) {
        int addNum = 0;
        for (Employee employee : list) {
            employeeMapper.insert(employee);
            addNum++;
        }
        return addNum;
    }

    public RespPageBean getEmployeeByPageWithSalary(Integer page, Integer size) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<Employee> list = employeeMapper.getEmployeeByPageWithSalary(page, size);
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setData(list);
        respPageBean.setTotal(employeeMapper.getTotal(null, null));
        return respPageBean;
    }

    public Integer updateEmployeeSalaryById(Integer eid, Integer sid) {
        return employeeMapper.updateEmployeeSalaryById(eid, sid);
    }
}
